<?php

/**
 * collection actions.
 *
 * @package    Serag
 * @subpackage collection
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class collectionActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeAddCollection(sfWebRequest $request)
  {
    $form = $request->getParameter('collection',new CollectionForm());
    $isCheque = (@$form['pay_type'] == 'cheque')?true:false;
    
    $this->form = new CollectionForm($isCheque);

        if ($request->isMethod(sfRequest::POST)) {
            $this->form->bind($request->getParameter($this->form->getName()),$request->getFiles($this->form->getName()));

            if ($this->form->isValid()) {
                $fields = $request->getParameter($this->form->getName());
                $product = new Collection();
                $product->setUserId($this->getUser()->getAttribute('User')->getUserCodeNum());
                $product->setCreatedAt(date("Y-m-d H:i:s"));
                if($fields['pay_type'] != 'cheque'){
                    $fields['cheque_number'] = $fields['due_date'] = null;
                }
                $product->fromArray($fields);
                $product->setDate(date("Y-m-d H:i:s", strtotime($fields['date']." ".date('H:i:s'))));
				$product->save();
                return $this->renderText('Done');
            }
        } 
  }
  public function executeEditCollection(sfWebRequest $request)
  {
    $this->collectionCode = $request->getParameter('id');
    $collectionData = Collection::getCollectionDetails($this->collectionCode);
    $collectionData['date'] = date('Y-m-d', strtotime($collectionData['date']));
    $form = $request->getParameter('collection', $collectionData);
    $isCheque = (@$form['pay_type'] == 'cheque')?true:false;
    
    $this->form = new CollectionForm($isCheque, $this->collectionCode);
    $this->form->setDefaults($collectionData);
    
        if ($request->isMethod(sfRequest::POST)) {
            $this->form->bind($request->getParameter($this->form->getName()),$request->getFiles($this->form->getName()));

            if ($this->form->isValid()) {
                $fields = $request->getParameter($this->form->getName());
                $product = Doctrine::getTable('Collection')->findOneByCollectionCode($this->collectionCode);
                $product->setUserId($this->getUser()->getAttribute('User')->getUserCodeNum());
                $product->setCreatedAt(date("Y-m-d H:i:s"));
                if($fields['pay_type'] != 'cheque'){
                    $fields['cheque_number'] = $fields['due_date'] = null;
                }
                $product->fromArray($fields);
                $product->setDate(date("Y-m-d H:i:s", strtotime($fields['date']." ".date('H:i:s'))));
				$product->save();
                return $this->renderText('Done');
            }
        } 
  }
}
